﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 28/12/2008
// Time: 16:40
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Collections.Generic;

namespace csharp.rad.general
{
	/// <summary>
	/// Description of Person.
	/// </summary>
	public class Person : csharp.common.DefaultClass, csharp.common.IClass
	{
		#region private declarations 
			private string _name; 
			private string _nickname;
			private string _cpf_cnpj; 
			private string _rg_ie; 
			private string _born_date; 
			private string _register_date; 
			private string _nationality; 
			private string _naturalness; 
			private string _marital_status; 
			private string _sex; 
			private string _email; 
			private string _status; 
			private string _enable; 
			private string _code; 
			private string _name_responsable; 
			private string _document_responsable; 
			private string _username; 
			private string _password; 
			private string _person_id; 
			private string _type_id; 
			private string _web_page; 
			private string _obs; 
			private string _update_date;             	
			private string _juridic;
			private string _company_id;
		#endregion
		
		public Person()
		{
			clear();
			gsql.table = "common.person";
			_sequence  = "common.person_person_id_seq";
			
			companyId = Information.companyId;
			
		}

		#region properties
			public string name 
			{
				get { return _name; }
				set { _name = value; }
			}
			
			public string nickname
			{
				get { return  _nickname; }
				set { _nickname = value; }
			}
			
			public string cpfCnpj 
			{
				get { return _cpf_cnpj; }
				set { _cpf_cnpj = value; }
			}
			
			public string rgIe 
			{
				get { return _rg_ie; }
				set { _rg_ie = value; }
			}
			
			public string bornDate 
			{
				get { return _born_date; }
				set { _born_date = value; }
			}
			
			public string registerDate 
			{
				get { return _register_date; }
				set { _register_date = value; }
			}
			
			public string nationality 
			{
				get { return _nationality; }
				set { _nationality = value; }
			}
			
			public string naturalness 
			{
				get { return _naturalness; }
				set { _naturalness = value; }
			}
			
			public string maritalStatus 
			{
				get { return _marital_status; }
				set { _marital_status = value; }
			}
			
			public string sex 
			{
				get { return _sex; }
				set { _sex = value; }
			}
			
			public string email 
			{
				get { return _email; }
				set { _email = value; }
			}
			
			public string status 
			{
				get { return _status; }
				set { _status = value; }
			}
			
			public bool enable 
			{
				get { return Convert.ToBoolean(_enable); }
				set { _enable = Convert.ToString(value); }
			}
			
			public string code 
			{
				get { return _code; }
				set { _code = value; }
			}
			
			public string nameResponsable 
			{
				get { return _name_responsable; }
				set { _name_responsable = value; }
			}
			
			public string documentResponsable 
			{
				get { return _document_responsable; }
				set { _document_responsable = value; }
			}
			
			public string username 
			{
				get { return _username; }
				set { _username = value; }
			}
			
			public string password 
			{
				get { return _password; }
				set { _password = value; }
			}
			
			public string personId 
			{
				get { return _person_id; }
				set { _person_id = value; }
			}
			
			public string typeId 
			{
				get { return _type_id; }
				set { _type_id = value; }
			}
			
			public string webPage 
			{
				get { return _web_page; }
				set { _web_page = value; }
			}
			
			public string obs 
			{
				get { return _obs; }
				set { _obs = value; }
			}
			
			public string updateDate 
			{
				get { return _update_date; }
				set { _update_date = value; }
			}		
		
			public bool juridic 
			{
				get { return Convert.ToBoolean(_juridic); }
				set { _juridic = Convert.ToString(value); }
			}		
			
		public string companyId 
		{
			get { return _company_id; }
			set { _company_id = value; }
		}
			
		#endregion
		
		public void clear()
		{
			_id						= String.Empty; 
			_name						= String.Empty; 
			_nickname				= string.Empty;
			_cpf_cnpj				= String.Empty; 
			_rg_ie					= String.Empty; 
			_born_date				= String.Empty; 
			_register_date			= String.Empty; 
			_nationality			= String.Empty; 
			_naturalness			= String.Empty; 
			_marital_status		= String.Empty; 
			_sex						= String.Empty; 
			_email					= String.Empty; 
			_status					= String.Empty; 
			_enable 					= Boolean.FalseString; 
			_code						= String.Empty; 
			_name_responsable		= String.Empty; 
			_document_responsable= String.Empty; 
			_username				= String.Empty; 
			_password				= String.Empty; 
			_person_id				= String.Empty; 
			_type_id					= String.Empty; 
			_web_page				= String.Empty; 
			_obs						= String.Empty; 
			_update_date			= DateTime.Now.ToString("yyyy/MM/dd");
			_juridic 				= Boolean.FalseString;
			_company_id				= String.Empty;
		}
	
		public override void get(System.Data.DataRow dr)
		{
			_id						= dr["person_id"].ToString(); 
			_name						= dr["person_name"].ToString(); 
			_nickname				= dr["person_nickname"].ToString();
			_cpf_cnpj				= dr["person_cpf_cnpj"].ToString(); 
			_rg_ie					= dr["person_rg_ie"].ToString(); 
			_born_date				= dr["person_born_date"].ToString(); 
			_register_date			= dr["person_register_date"].ToString(); 
			_nationality			= dr["person_nationality"].ToString(); 
			_naturalness			= dr["person_naturalness"].ToString(); 
			_marital_status		= dr["person_marital_status"].ToString(); 
			_sex						= dr["person_sex"].ToString(); 
			_email					= dr["person_email"].ToString(); 
			_status					= dr["person_status"].ToString(); 
			_enable 					= dr["person_enable"].ToString(); 
			_code						= dr["person_code"].ToString(); 
			_name_responsable		= dr["person_name_responsable"].ToString(); 
			_document_responsable= dr["person_document_responsable"].ToString(); 
			_username				= dr["person_username"].ToString(); 
			_password				= dr["person_password"].ToString(); 
			_person_id				= dr["person_person_id"].ToString(); 
			_type_id					= dr["person_type_id"].ToString(); 
			_web_page				= dr["person_web_page"].ToString(); 
			_obs						= dr["person_obs"].ToString(); 
			_update_date			= dr["person_update_date"].ToString();	
			_juridic					= dr["person_juridic"].ToString();
			_company_id				= dr["person_company_id"].ToString();
		}
		
		public override void setGsql()
		{
			gsql.setValue(_id						, "person_id"); 
			gsql.setValue(_name					, "person_name"); 
			gsql.setValue(_nickname				, "person_nickname");
			gsql.setValue(_cpf_cnpj				, "person_cpf_cnpj"); 
			gsql.setValue(_rg_ie					, "person_rg_ie"); 
			gsql.setValue(Convert.ToDateTime(_born_date).ToString("yyyy/MM/dd")			, "person_born_date");
			//gsql.setValue(Convert.ToDateTime(_register_date).ToString("yyyy/MM/dd")		, "person_register_date"); 
			gsql.setValue(_nationality			, "person_nationality"); 
			gsql.setValue(_naturalness			, "person_naturalness"); 
			gsql.setValue(_marital_status		, "person_marital_status"); 
			gsql.setValue(_sex					, "person_sex"); 
			gsql.setValue(_email					, "person_email"); 
			gsql.setValue(_status				, "person_status"); 
			gsql.setValue(_enable 				, "person_enable"); 
			gsql.setValue(_code					, "person_code"); 
			gsql.setValue(_name_responsable		, "person_name_responsable"); 
			gsql.setValue(_document_responsable	, "person_document_responsable"); 
			gsql.setValue(_username				, "person_username"); 
			gsql.setValue(_password				, "person_password"); 
			gsql.setValue(_person_id			, "person_person_id"); 
			gsql.setValue(_type_id				, "person_type_id"); 
			gsql.setValue(_web_page				, "person_web_page"); 
			gsql.setValue(_obs					, "person_obs"); 
			gsql.setValue(Convert.ToDateTime(_update_date).ToString("yyyy/MM/dd")			, "person_update_date");     
			gsql.setValue(_juridic				, "person_juridic");
			gsql.setValue(_company_id			, "person_company_id");
		}
	
	}

	//List
	public class Persons : csharp.common.DefaultList, csharp.common.IList
	{
		public Persons()
		{
			gsql.table= "common.person";
		}		
		
		public Person get()
		{
			return (Person)items[index];
		}
		
		public Person get(Int32 index)
		{
			return (Person)items[index];
		}
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.conditionalWhere = "person_company_id = "+Information.companyId;
			//gsql.additionalWhere = "ORDER BY person_name";
			
			return mountList();
		}
		
		public bool fillListOrderByName()
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.conditionalWhere = "person_company_id = "+Information.companyId;
			gsql.additionalWhere = "ORDER BY person_name";
			
			return mountList();
		}	
		

		
		private bool mountList()
		{
			Int32 tam;
			
			if (!gsql.execute())
				return false;
			
			tam= gsql.getRowCount();
			items.Clear();
			for (int i=0; i < tam; i++)
			{
				Person p = new Person();
				p.get(gsql.getRow(i));
				items.Add(p);
			}
			return true;
		}

		public bool searchByName(String value)
		{
			int tam= items.Count;
			
			value = value.ToUpper();
			for (int i=0; i < tam; i++)
			{
				if ( this.get(i).name.ToUpper().StartsWith(value))
				{
					index= i;
					return true;
				}
			}
			return false;
		}
	}   

}
